package com.mxchip.livestar.ui.dial

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.mxchip.livestar.R
import com.mxchip.livestar.base.getString
import com.mxchip.livestar.ui.Call
import com.mxchip.livestar.ui.GroupCall
import com.mxchip.livestar.ui.P2PSingleCall

class DialViewModel(application: Application) : AndroidViewModel(application) {

    private val mDialNumber = MutableLiveData<String>()

    private val mMyNumber = MutableLiveData<String>()

    val number: LiveData<String>
        get() = mDialNumber

    val myNumber: LiveData<String>
        get() = mMyNumber

    fun setMyNumber(number: String) {
        mMyNumber.value = number
    }

    fun appendDigit(digit: String) {

        val number = mDialNumber.value

        if (number.isNullOrBlank()) {
            mDialNumber.value = digit
        } else {
            if (number.length == 16) {
                mDialNumber.value = number
            } else {
                mDialNumber.value = (number + digit)
            }
        }
    }

    fun removeLastDigit() {

        val number = mDialNumber.value

        if (number.isNullOrBlank()) {
            return
        }

        mDialNumber.value = number.substring(0, number.length - 1)
    }

    fun clearAllDigits() {
        mDialNumber.value = null
    }


    fun makeCall(): Call {

        val number = mDialNumber.value

        if (number.isNullOrBlank()) {
            throw Throwable(getString(R.string.input_number))
        }

        val call = when {
            number.length <= 8 -> {

                val aNumber = number.padStart(8, '0')

                if (aNumber == myNumber.value) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(aNumber)
            }

            number.length == 11 -> {

                if (number == myNumber.value) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(number)
            }

            number.length == 12 -> {
                if (number.contains("#")) {
                    P2PSingleCall(number)
                } else {
                    GroupCall(number)
                }
            }

            number.length == 15 -> {
                if (number.contains("#")) {
                    P2PSingleCall(number)
                } else {
                    throw Throwable(getString(R.string.called_number_error))
                }
            }

            number.length == 16 -> {
                if (number.contains("#")) {
                    GroupCall(number)
                } else {
                    throw Throwable(getString(R.string.called_number_error))
                }
            }
            else -> {
                throw Throwable(getString(R.string.called_number_error))
            }
        }

        mDialNumber.value = null

        return call
    }
}